package com.app.ytong.record;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.ytong.R;
import com.app.ytong.basis.BasisActivity;
import com.app.ytong.basis.Constants;
import com.app.ytong.bean.BasisBean;
import com.app.ytong.bean.GoodsListData;
import com.app.ytong.bean.LoginBean;
import com.app.ytong.bean.RecordListBean;
import com.app.ytong.bean.RecordListData;
import com.app.ytong.bean.SupplierListData;
import com.app.ytong.bean.TraderListData;
import com.app.ytong.manager.qing.ScalePresenter;
import com.app.ytong.manager.qing.ScalePresenterImpl;
import com.app.ytong.manager.qing.ScaleView;
import com.app.ytong.util.MyUtils;
import com.caysn.autoreplyprint.AutoReplyPrint;
import com.tencent.bugly.crashreport.biz.UserInfoBean;
import com.vandin.weigher.sdk.connector.IWeigh;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import my.DialogUtils;
import my.MathUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class RecordInfoActivity extends BasisActivity implements ScaleView {

    ScalePresenter presenter;
    private IWeigh IControl;

    @BindView(R.id.text_wt)
    TextView text_wt;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    RecordListBean listBean;

    MyAdapterList mAdapter;

    String mid;
    String tid;

    boolean isChange =false;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.record_info_activity);
        setTitle("明细");
        setTitleBg(R.color.base_blue);
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        presenter = new ScalePresenterImpl(this);
        IControl = presenter.getIWeigh(mContext);

        setTitleLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isChange = true){
                    setResult(RecordListActivity.WARNLISTRESULT);
                }
                finish();
            }
        });

        setTitleRightButton(R.mipmap.icon_save_white, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listBean == null || listBean.getList().size()<0){
                    showToast("已经没有数据了");
                    return;
                }
                presenter.editRecord(RecordInfoActivity.this,listBean);
            }
        });

        setTitleRightButton2(R.mipmap.icon_printer, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listBean == null || listBean.getList().size()<0){
                    showToast("已经没有数据了");
                    return;
                }
                presenter.showRecord(RecordInfoActivity.this,listBean);
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new RecordListBean();
        mid = getIntent().getStringExtra("mid");
        tid = getIntent().getStringExtra("tid");
        if (TextUtils.isEmpty(mid)){
            showToast("mid不可为空");
            return;
        }

        if (TextUtils.isEmpty(tid)){
        }


        getList();
    }

    private void getList(){
        showLoading();
        MyParams params = new MyParams();
        params.put("aid", LoginBean.getInstance().getAid());
        params.put("mid",mid);
        if(!TextUtils.isEmpty(tid)){
            params.put("tid",tid);
        }

        String url ="";
        if(MyUtils.getTabSelectName().contains("入")){
            url = Constants.URL_INSTOCK_SELECT_LIST_BY_MID;
        }else {
            url = Constants.URL_OUTSTOCK_SELECT_LIST_BY_MID;
        }

        MyHttpUtils.get(url, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (RecordListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if (isFinishing())return;
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if (isFinishing())return;
                        dismissProgress();
                    }
                },
                0, RecordListBean.class);
    }


    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.record_info_list_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.view_pwt)
            public View list_pwt;
            @BindView(R.id.view_gnu)
            public View view_gnu;
            @BindView(R.id.view_rfid)
            public View list_rfid;
            @BindView(R.id.text_gna)
            public TextView text_gna;
            @BindView(R.id.text_tna)
            public TextView text_tna;
            @BindView(R.id.text_lna)
            public TextView text_lna;
            @BindView(R.id.text_buy_or_sup)
            public TextView text_buy_or_sup;
            @BindView(R.id.text_cna)
            public TextView text_cna;
            @BindView(R.id.text_gwt)
            public TextView text_gwt;
            @BindView(R.id.edit_rfid)
            public EditText edit_rfid;
            @BindView(R.id.edit_pwt)
            public EditText edit_pwt;
            @BindView(R.id.edit_pri)
            public EditText edit_pri;
            @BindView(R.id.text_total)
            public TextView text_total;
            @BindView(R.id.add_btn)
            public TextView add_btn;
            @BindView(R.id.text_gnu)
            public TextView text_gnu;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
                edit_pwt.removeTextChangedListener(mPwtWatcher);
                edit_pwt.addTextChangedListener(mPwtWatcher);

                edit_pri.removeTextChangedListener(mGprWatcher);
                edit_pri.addTextChangedListener(mGprWatcher);

                edit_rfid.removeTextChangedListener(mRfidWatcher);
                edit_rfid.addTextChangedListener(mRfidWatcher);

            }

            public void bind(int position) {
                RecordListData data = listBean.getList().get(position);
                MyUtils.setNotNullText(text_gna,""+data.getGna());
                MyUtils.setNotNullText(text_tna,""+data.getTna());
                MyUtils.setNotNullText(text_lna,""+data.getLna());
                MyUtils.setNotNullText(text_cna,""+data.getCna());
                MyUtils.setNotNullText(edit_pri,""+data.getPri());
                MyUtils.setNotNullText(text_total,""+data.getMoney());
                MyUtils.setNotNullText(text_gwt,""+data.getGwt());
                if(MyUtils.getTabSelectName().contains("出")){
                    text_buy_or_sup.setText("客户");
                }
                if(MyUtils.getHomeSelectName().contains("茶青") && MyUtils.getTabSelectName().contains("入")){
                    list_pwt.setVisibility(View.VISIBLE);
                    list_rfid.setVisibility(View.GONE);
                    MyUtils.setNotNullText(edit_pwt,""+data.getPwt());
                }else {
                    list_pwt.setVisibility(View.GONE);
                    list_rfid.setVisibility(View.VISIBLE);
                    MyUtils.setNotNullText(edit_rfid,""+data.getRfid());
                }

                if(MyUtils.getHomeSelectName().contains("饼")){
                    view_gnu.setVisibility(View.VISIBLE);
                    MyUtils.setNotNullText(text_gnu,""+data.getGnu());
                }else {
                    view_gnu.setVisibility(View.GONE);
                }

                list_item.setTag(data);
                add_btn.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {


            }

            @OnLongClick(R.id.list_item)
            public boolean onItemLongClick(View v) {
                RecordListData data = (RecordListData) v.getTag();
                DialogUtils.showWarningDialog(mContext,"是否删除","确定", new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        MyParams params = new MyParams();
                        showProgress();
                        params.put("aid", LoginBean.getInstance().getAid());
                        params.put("fsg", MyUtils.getPutTabSelectId());
                        params.put("iid", data.getIid());
                        String url ="";
                        if(MyUtils.getTabSelectName().contains("入")){
                            url = Constants.URL_INSTOCK_DELETE_SUB;
                        }else {
                            url = Constants.URL_OUTSTOCK_DELETE_SUB;
                        }
                        MyHttpUtils.delete(url, params, new MyHttpListener() {
                                    @Override
                                    public void onSuccess(int httpWhat, Object result) {
                                        if (isFinishing()) return;
                                        BasisBean basisBean = (BasisBean) result;
                                        showToast(basisBean.getStatusInfo());
                                        isChange = true;
                                        listBean.getList().remove(data);
                                        notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onFailure(int httpWhat, Object result) {
                                        super.onFailure(httpWhat, result);
                                    }

                                    @Override
                                    public void onFinish(int httpWhat) {
                                        dismissProgress();

                                    }
                                },
                                0, BasisBean.class);
                    }
                });
                return true;
            }

            @OnClick(R.id.add_btn)
            public void onAddClick(View v) {
                RecordListData data = (RecordListData) v.getTag();
                if(TextUtils.isEmpty(edit_pwt.getText().toString())){
                    edit_pwt.setText("0");
                }
                edit_pwt.setText(MathUtils.add(data.getPwt(),presenter.getScaleWeight()));
            }

            TextWatcher mPwtWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    int position = getAdapterPosition();
                    RecordListData data = listBean.getList().get(position);
                    if(s.toString().equals("") || TextUtils.isEmpty(s.toString()) || "null".equals(s.toString())){
                        data.setPwtAndNotify("0");
                    }else {
                        data.setPwtAndNotify(s.toString());
                    }
                }
            };
            TextWatcher mGprWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    int position = getAdapterPosition();
                    RecordListData data = listBean.getList().get(position);
                    if(s.toString().equals("") || TextUtils.isEmpty(s.toString()) || "null".equals(s.toString())){
                        data.setPriAndNotify("0");
                    }else {
                        data.setPriAndNotify(s.toString());
                    }
                }
            };
            TextWatcher mRfidWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    int position = getAdapterPosition();
                    RecordListData data = listBean.getList().get(position);
                    if(s.toString().equals("") || TextUtils.isEmpty(s.toString()) || "null".equals(s.toString())){
                        data.setRfid("0");
                    }else {
                        data.setRfid(s.toString());
                    }
                }
            };
        }
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.closeIWeigh();
    }


    @Override
    public void isJiJiaYuan(boolean yesOrNo, TraderListData data) {

    }

    @Override
    public void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getGoodsOptionsResult(GoodsListData goodsListData) {

    }

    @Override
    public void getSupplierResult(SupplierListData supplierListData) {

    }

    @Override
    public void settlementSuccess() {
        isChange = true;
        setListView();
    }

    @Override
    public void dashboardViewSetValue(float value) {
        runOnUiThread(new Runnable() {   //执行UI更新
            @Override
            public void run() {
                text_wt.setText(""+value);
            }
        });
    }

    @Override
    public void printMessage(String message) {

    }

    @Override
    public void printFinish() {

    }

}
